package flow.action;

import flow.model.FlowInstance;
import flow.model.FlowInstanceSection;
import flow.service.FlowInstanceService;
import m.common.action.ActionMeta;
import m.common.action.VueResult;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.list.*;
import m.system.util.JSONMessage;
import manage.action.ManageAction;

@ActionMeta(name="flow/flowInstance")
public class FlowInstanceAction extends ManageAction {
	private FlowInstance model;

	public JSONMessage getInstance() throws Exception {
		return getService(FlowInstanceService.class).getInstance(model);
	}

	public VueResult flowInstanceList() throws Exception{
		verifyMenu("A3111");
		boolean power=hasAdminOperPower("manage_flow_power");
		VueListResult result=getVueListResult("流程实例",FlowInstance.class,actionUrl("flowInstanceList"));
		result.setSubTitle("流程实例列表");
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("业务类型", "business.type",120,true).setSort(true)
			.addClassName("record.doneStatus=='Y'", "text-green").addClassName("record.doneStatus=='N'", "text-red"));
		result.addCol(toLinkColumn("业务标题", "business.title",230,true,
			new ListEvent("page/base/flow/flowInfo.js", EventType.MODAL,1200).addParam("business.oid","business.oid")
		));
		result.addCol(toLinkColumn("流程", "flowDefine.identity",200,true,
			new ListEvent(actionUrl(FlowDefineAction.class,"toEdit"), EventType.MODAL,1200).addParam("model.oid","flowDefine.oid")
		).setFieldExpression("concat(#{flowDefine.name},'(',#{flowDefine.identity},') v',#{flowDefine.version})"));
		result.addCol(toLinkColumn("当前环节", "currentSection.identity",150,true,
			new ListEvent("page/base/flow/viewFlowDefine.js", EventType.MODAL,800).addParam("instanceOid","oid").addParam("defineOid","flowDefine.oid")
		).setFieldExpression("concat(#{currentSection.identity},'(',#{currentSection.name},')')"));
		result.addCol(toDateColumn("创建时间", "createDate",120,true).setSort(true));
		result.addCol(toTextColumn("完成状态", "doneStatus",true).setAlign(Align.CENTER).setDictOption(FlowInstance.class,"doneStatus")
			.addElement(ListElement.test("text=='Y'", ListElement.tag("{{dict('flow.model.FlowInstance!doneStatus',text)}}","success",false)))
			.addElement(ListElement.elseOther(ListElement.tag("{{dict('flow.model.FlowInstance!doneStatus',text)}}","error",true)))
		);
		result.addCol(toDateColumn("完成时间", "doneDate",120,true).setSort(true));
		if(power) result.addCol(toSpaceColumn("操作",120,
			ListButton.modal("执行情况", ButtonType.PRIMARY,actionUrl("flowInstanceSectionList"),900, SuccessMethod.REFRESH)
				.addParam("params[flowInstance.oid]","oid")
		));
		result.addCondition(QueryCondition.in("business.doneStatus","Y","N"));
		return result;
	}

	public VueResult flowInstanceSectionList() throws Exception{
		verifyMenu("A3111");
		VueListResult result=getVueListResult("流程实例环节",FlowInstanceSection.class,actionUrl("flowInstanceSectionList"));
		result.setSubTitle("流程实例环节列表");
		result.addCol(toIndexColumn());
		result.addCol(toTextColumn("索引", "flowIndex",60).setSort(ListSortOrder.ASC));
		result.addCol(toTextColumn("环节", "flowSection.identity",150,true).setFieldExpression("concat(#{flowSection.identity},'(',#{flowSection.name},')')"));
		result.addCol(toTextColumn("处理人", "user.realname",90,true));
		result.addCol(toDateColumn("接收时间", "acceptDate",true).setSort(true));
		result.addCol(toTextColumn("完成状态", "doneStatus",true).setAlign(Align.CENTER).setDictOption(FlowInstanceSection.class,"doneStatus"));
		result.addCol(toDateColumn("完成时间", "doneDate",true).setSort(true));
		result.addSearch(ListSearch.field("","flowInstance.oid", SearchType.HIDDEN_EQ));
		result.addOrder(QueryOrder.desc("doneDate"));
		return result;
	}

	public FlowInstance getModel() {
		return model;
	}

	public void setModel(FlowInstance model) {
		this.model = model;
	}
	
}
